<?php

/**
 * 转换数据
 *
 * PHP version 7.2
 *
 * @category  API
 * @package   YII
 * @author    saruri <saruri@163.com>
 * @copyright 2006-2019 saruri
 * @license   https://saruri.cn/licence.txt BSD Licence
 * @link      http://saruri.cn
 * @date      2020/04/12 11:28:29
 */
namespace saruri;

//use Yii;
class DataTransform
{
    private $_config = [];
    //初始化
    public function __construct($_config)
    {
        //各种配置
        $this->_config=$_config;
        $this->init();
    }
      
    //继承上级的初始化
    public function init()
    {
        //可选
        //$this->_config['requestString']=str_replace("#","",$this->_config['requestString']);
        //$this->_config['requestString']=str_replace(">","",$this->_config['requestString']);
        //$this->_config['requestString']=str_replace("*","",$this->_config['requestString']);
        //$this->arrResult=explode("\n",$this->_config['requestString']);
        $this->result['msg']='初始化了';
        $this->result['data']=[];
    }
      
    /*
    *  @desc      返回参数
    *  @author    saruri <saruri@163.com>
    *  @date      2020/10/21 11:27:36
    */
    public function run()
    {
        //do somehing
        return $this->td;
        $arr['data']=$this->result['result']. $this->_config['requestString'];
        return  $arr;
    }


    /*
    *  @desc      layui代码
    *  @author    saruri <saruri@163.com>
    *  @date      2020/10/22 13:36:04
    */
    public function layui()
    {
        //do somehing
        $string=self::layuiBase();
        return $string;
    }

    /*
    *  @desc      循环输出
    *  @author    saruri <saruri@163.com>
    *  @date      2020/10/22 13:37:40
    */
    public function layuiBase()
    {
        //do somehing
        //var_dump($this->td);
        //exit("测试lyui");
        $reusltLayui='<style>.layui-form-item .layui-input-inline { width: 140px;}.layui-form-item .layui-inline {
            margin-bottom: 5px;
            margin-right: 10px;
            min-width: 15%;
            text-align: center;
        }</style>';
        foreach ($this->td as $key => $value) {
            # code...
            $reusltLayui.="<div class=\"layui-form-item\">"."\r\n";
            $reusltLayui.=self::layuiTr($value);
            $reusltLayui.="\r\n"."</div>"."\r\n";
        }
        return $reusltLayui;
    }

    /*
    *  @desc      构建layui的 一行元素
    *  @author    saruri <saruri@163.com>
    *  @date      2020/10/22 13:48:57
    */
    public function layuiTr(array $data)
    {
        //判断数组情况
        //单一情况

        // <div class="layui-inline">
        // <label class="layui-form-label label-required-next">密码</label>
        // <div class="layui-input-inline" style="width: 100px;">
        // <input type="text" name="teacher_name" maxlength="50" required="" lay-verify="required" value="" placeholder="请输入教师的姓名 必填" autocomplete="off" class="layui-input">
        // </div>
        // </div>
        $string='这里是layui的布局';
        $string=implode("|", $data);
        $string=json_encode($data);
        //return $string;
        $string='';

        $count=count($data);
        //判断是否单格 单格做成横线
        //return $count;
        //exit($count);
        if ($count==1) {
            return self::layuiHr($data);
        }

        foreach ($data as $key => $value) {
            # code...
            //$string.="<div class=\"layui-inline\">"."\r\n";
            //$string.="<label class=\"layui-form-label\">密码</label>
            // $string.=self::layuiLable($value);
           
            $string.=self::layuiTd($value, $data[$key+1]);

            //$string.="<div class=\"layui-input-inline\">"."\r\n";
            //$string.="<input type=\"text\" name=\"\" autocomplete=\"off\" class=\"layui-input\">"."\r\n";
            //$string.="</div>"."\r\n";
           // $string.="</div>"."\r\n";
           // $string.="\r\n";
        }
        return $string;
    }
  
    /*
    *  @desc      单格输出
    *  @author    saruri <saruri@163.com>
    *  @date      2020/10/22 15:43:33
    */
    public function layuiHr(array $data, array $arr =[])
    {
        //do somehing
        $str=implode("|", $data);
        $str=json_encode($data);
        //return "<h3 class=\"site-title\">".$data[0]['text']."</h3>";
        $string.='<div class="site-title">'."\r\n";
        $string.='<fieldset><legend><a name="use">'.$data[0]['text'].'</a></legend></fieldset>'."\r\n";
        // $string.= $str."\r\n";
        $string.='</div>'."\r\n";
        
        return $string;
    }

    /*
    *  @desc      输出td
    *  @author    saruri <saruri@163.com>
    *  @date      2020/10/22 15:14:24
    */
    public function layuiTd(array $data)
    {
        //判断条件
        //$count=count($data);
        //return $count;
        //数组 多元素 空白  下格文字
        //数组 多元素 1文字 下格空白
        //数组 多元素 1文字 下格文字
        //数组 多元素 1文字 下格没有 行尾
        $string='';

        if (isset($data['text']) and $data['text']) {
            //$string.="<div class=\"layui-inline layui-col-md1\">"."\r\n";
            $string.="<div class=\"layui-inline\">"."\r\n";
            $string.= "<label class=\"layui-form-label\">".$data['text']."</label>"."\r\n";
            //$string.=$data['text']." 文字\r\n";
            $string.='</div>'."\r\n";
            return $string;
        }

        //$string.="<div class=\"layui-inline layui-col-md1 \">"."\r\n";
        $string.="<div class=\"layui-inline  \">"."\r\n";
        //$string.="<label class=\"layui-form-label\">密码</label>
        // $string.=self::layuiLable($value);
           
        //$string.=self::layuiTd($value, $data[$key+1]);

        $string.="<div class=\"layui-input-inline\">"."\r\n";
        $string.="<input type=\"text\" name=\"\" autocomplete=\"off\" value=\"请补足name\" class=\"layui-input\">"."\r\n";
        $string.="</div>"."\r\n";
        $string.="</div>"."\r\n";
        $string.="\r\n";
        return $string;


        //数组 氮元素  带文字
        //数组 单元素  不带文字
        //
    }


    /*
    *  @desc      构造lable 说明
    *  @author    saruri <saruri@163.com>
    *  @date      2020/10/22 14:30:51
    */
    public function layuiLable(array $data)
    {
        //do somehing
        if (isset($data['text'])) {
           
            return "<label class=\"layui-form-label\">".$data['text']."</label>"."\r\n";
        }
        //return "<label class=\"layui-form-label\">".$data['text']."</label>"."\r\n";
    }

    /*
    *  @desc      strip
    *  @author    saruri <saruri@163.com>
    *  @date      2020/10/21 13:47:05
    */
    public function strip()
    {
        //do somehing
        $string=str_replace("&nbsp;", "", $this->_config['requestString']);
        preg_replace("/(\s|\&nbsp\;|　|\xc2\xa0)/", " ", $string);
        $string=str_replace("valign=\"center\"", "", $this->_config['requestString']);

        $exp=array("/height=.{0,5}\s/i","/width=.{0,5}\s/i");
        $exp_o=array('','');
        $string=preg_replace($exp, $exp_o, $string);


        $string=str_replace("&nbsp;", "", $string);
        //$string=str_replace("</td >", "</td>", $string);
        //$string=str_replace("<td >", "<td>", $string);

        //$string=str_replace(",,", "", $string);
        //$res = preg_replace('/style=".*?"/i', '', $html); //去掉style
        $string=strip_tags($string, '<table>,<td>,<tr>,<th>');
        
        $this->_config['requestString']=$string;
        return $this;
    }


    /*
    *  @desc      输出纯表格
    *  @author    saruri <saruri@163.com>
    *  @date      2020/10/22 11:17:28
    */
    public function tab()
    {
        //do somehing
        $str="<table class=\"MsoTableGrid layui-table\" border=\"1\" cellspacing=\"0\">"."\r\n";
        $str2="</table>";
        // $str2=str_replace("</tr>","</tr>"."\r\n",$arr[1]);
        return $str.$this->_configTab['requestString'].$str2;
    }

       /*
    *  @desc      输出表格+文本框
    *  @author    saruri <saruri@163.com>
    *  @date      2020/10/22 11:17:28
    */
    public function tabInput()
    {
        //do somehing
        $str="<table class=\"MsoTableGrid layui-table\" border=\"1\" cellspacing=\"0\">"."\r\n";
        $str.=self::addInput();
        $str.="</table>"."\r\n";
        $str.=" <style>"."\r\n";
        $str.=" .layui-form input.my-layui-input {
            line-height: 1em;
            color: #b1afaf;
            border: 1px solid #fff;
        } "."\r\n";
        $str.="  input::-webkit-input-placeholder{
            color:#ddd;
        }
        input::-moz-placeholder{   /* Mozilla Firefox 19+ */
            color:#ddd;
        }
        input:-moz-placeholder{    /* Mozilla Firefox 4 to 18 */
            color:#111;
        }
        input:-ms-input-placeholder{  /* Internet Explorer 10-11 */ 
            color:#111;
        }"."\r\n";

        $str.="</style>"."\r\n";
        // $str2=str_replace("</tr>","</tr>"."\r\n",$arr[1]);
        return $str;
    }


    /* 
    *  @desc      给空td 增加 input
    *  @author    saruri <saruri@163.com>
    *  @date      2020/10/23 09:44:00  
    */ 
    public function  addInput()
    {
        //do somehing
        $str='>'."\r\n".'<input name="" value="" placeholder="请输入文本框的name 和值 必填" type="text"  maxlength="50" required="" lay-verify="required"   autocomplete="off" class="layui-input my-layui-input">'."\r\n".'</td>';
        $this->_configTab['requestString']=str_replace("></td>",$str,$this->_configTab['requestString']);
        return $this->_configTab['requestString'];
         
    }


    /*
    *  @desc      构造头部
    *  @author    saruri <saruri@163.com>
    *  @date      2020/10/21 11:35:43
    */
    public function th()
    {
        //do somehing
        $this->result['data']['tr']='';//'已经构造了tr';
        preg_match_all('/<tr(.*?)>(.*?)<\/tr>/s', $this->_config['requestString'], $matches);
        $data = $matches[2];
        $this->_config['requestString']='';
        for ($i=0,$len=count($data); $i<$len; $i++) {
            $this->tr[$i]=$data[$i];
            $this->_config['requestString'].=$data[$i]."\r\n";
            $this->_configTab2['requestString'].="<tr>\r\n".$data[$i]."\r\n</tr>\r\n";
            $this->_configTab['requestString'].="<tr>".$data[$i]."</tr>\r\n";
        }
        return $this;
    }

    /*
  *  @desc      构造单元格
  *  @author    saruri <saruri@163.com>
  *  @date      2020/10/21 11:35:43
  */
    public function td()
    {
        foreach ($this->tr as $key => $value) {
            # code...
            $this->td[$key]=$this->getTd($value);
        }
       
        //do somehing
        $this->result['data']['td']='';//'已经构造了td';
        return $this;
    }

    

    /*
    *  @desc      /构造单元格数组
    *  @author    saruri <saruri@163.com>
    *  @date      2020/10/21 15:19:04
    */
    public function getTd(string $string)
    {
        //do somehing
        // return $string;

        $arr2=explode("</td>", $string);
        foreach ($arr2 as $key => $value) {
            # code...
            if ($value) {
                $arr[$key]['text']=$this->getTdText($value);
                $arr[$key]['col']=$this->getTdCol($value);
                $arr[$key]['row']=$this->getTdRow($value);
            }
           
            //$arr[$key]['val']=$value;
            //$arr[$key]['str']=$string;
            //$arr[$key]['arr']=implode("|", $arr2);
        }

        return $arr;
    }


    /*
    *  @desc      获取单元格正文
    *  @author    saruri <saruri@163.com>
    *  @date      2020/10/21 15:57:03
    */
    public function getTdText(string $string)
    {
        //do somehing
        $arr=explode(">", $string);
        return $arr[1];
        //return $string;
    }

    /*
    *  @desc      获取单元格正文
    *  @author    saruri <saruri@163.com>
    *  @date      2020/10/21 15:57:03
    */
    public function getTdCol(string $string)
    {
        //do somehing
        $arr=explode("colspan=\"", $string);
        $arr2=explode("\"", $arr[1]);

        return $arr2[0];
        return $string;
    }

    /*
    *  @desc      获取单元格正文
    *  @author    saruri <saruri@163.com>
    *  @date      2020/10/21 15:57:03
    */
    public function getTdRow(string $string)
    {
        //do somehing
        $arr=explode("rowspan=\"", $string);
        $arr2=explode("\"", $arr[1]);

        return $arr2[0];
        return $string;
    }

    /*
    *  @desc      基础表格样式
    *  @author    saruri <saruri@163.com>
    *  @date      2020/10/21 11:38:14
    */
    public function base(string $type)
    {
        //do somehing
        $this->type=$type;
        $this->result['data']['base']='';//'已经构造基础表格样式';
        return $this;
    }

    /*
    *  @desc      混合计算
    *  @author    saruri <saruri@163.com>
    *  @date      2020/10/21 11:50:37
    */
    public function com()
    {
        //do somehing
    
        $this->result['result']=$this->_config['requestString'];//implode(",", $this->result['data']);

        return $this;
    }
}
